package com.google.code.sip.ucenter.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.code.sip.ucenter.dao.mongo.FriendDaoImpl;
import com.google.code.sip.ucenter.dao.mongo.FriendGroupDaoImpl;
import com.google.code.sip.ucenter.dao.mongo.UserDaoImpl;
import com.google.code.sip.ucenter.model.Friend;
import com.google.code.sip.ucenter.model.FriendGroup;
import com.google.code.sip.ucenter.model.User;
import com.google.code.sip.ucenter.service.FriendManager;

@Service("friendManager")
public class FriendManagerImpl implements FriendManager {

	@Autowired
	private FriendDaoImpl friendDao;
	@Autowired
	private FriendGroupDaoImpl friendGroupDao;
	@Autowired
	private UserDaoImpl userDao;

	@Override
	public void addFriend(User user, User friendUser, FriendGroup friendGroup,
			String remark) {
		Friend friend = new Friend();
		if (user != null && friendUser != null && friendGroup != null) {
			friend.setUser(user);
			friend.setFriend(friendUser);
			friend.setFriendGroup(friendGroup);
			friend.setRemark(remark);
			friend.setCreatedDate(new Date());
			friendDao.save(friend);
		}
	}

	@Override
	public void deleteFriend(User user, User friendUser) {
		Friend friend = friendDao.getFriend(user, friendUser);
		if (friend != null) {
			friendDao.delete(friend);
		}

	}

	@Override
	public List<Friend> getFriends(User user) {
		return friendDao.getFriends(user);
	}

	@Override
	public List<Friend> getFriends(FriendGroup friendGroup) {
		return friendDao.findByProperty("friendGroup", friendGroup);
	}

	@Override
	public int getFriendsCount(User user) {

		return this.getFriends(user).size();
	}

	@Override
	public boolean isFriend(User user, User friendUser) {
		Friend friend = friendDao.getFriend(user, friendUser);
		if (friend != null) {
			return true;
		}
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.google.code.sip.ucenter.service.FriendManager#getFriendsWithMap(java.
	 * lang.String)
	 */
	@Override
	public Map<FriendGroup, List<User>> getFriendsWithMap(User user) {

		List<Friend> friendList = getFriends(user);
		Map<FriendGroup, List<User>> friendMap = new HashMap<FriendGroup, List<User>>();
		if (friendList.size() != 0) {
			Set<String> friendGroupIds = new HashSet<String>();
			Set<String> userIds = null;
			// 筛选好友组
			for (Friend friend : friendList) {
				friendGroupIds.add(friend.getFriendGroup().getId());
			}
			// 筛选每个好友组里面的好友
			for (String friendGroupId : friendGroupIds) {
				userIds = new HashSet<String>();
				for (Friend friend : friendList) {
					if (friend.getFriendGroup().equals(friendGroupId)) {
						userIds.add(friend.getFriend().getId());
					}
				}

				FriendGroup friendGroup = friendGroupDao.get(friendGroupId);
				if (userIds.size() == 0) {
					friendMap.put(friendGroup, null);
				} else {
					friendMap.put(friendGroup, userDao
							.findWithIn("id", userIds));
				}
			}

		}
		return friendMap;
	}

	@Override
	public void moveFriend(Friend friend, FriendGroup newFriendGroup) {
		friend.setFriendGroup(newFriendGroup);
		friendDao.save(friend);
	}

	@Override
	public Friend getFriend(User user, User friendUser) {
		return friendDao.getFriend(user, friendUser);
	}
}
